No.
The non-empty cells of an ArrayList
must contain object references (or null
).
To put an int
into an ArrayList
,
put the int
inside of an Integer
object.
Now the object can be put into an ArrayList
.
The following program builds a list of integers and then writes them out.
import java.util.* ; public class WrapperExample { public static void main ( String[] args) { ArrayList<Integer> data = new ArrayList<Integer>(); data.add( new Integer(1) ); data.add( new Integer(3) ); data.add( new Integer(17) ); data.add( new Integer(29) ); for ( Integer val : data ) System.out.print( val + " " ); System.out.println( ); } }
The program writes out:
1 3 17 29
The picture emphasizes that the ArrayList
contains an array of object
references, as always.
It shows the int
s each contained in a little box that
represents the wrapper object.
Would this statement work in the program?
data.add( 44 );